package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.vo.DetailsVo;
import com.dongdongshop.pojo.vo.GoodsVo;
import com.dongdongshop.service.goods.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("good")
public class GoodsController {

    //开启日志
    Logger logger = LoggerFactory.getLogger(SellerController.class);

    @Reference(check=false ,timeout=1006666)
    private GoodsService goodsService;

    //跳转到商品审核页面
    @RequestMapping("togoods")
    public String goodsList(){
        return "goods/goods";
    }
    //获取商品审核的商品信息
    @RequestMapping("getGoodsInfo")
    @ResponseBody
    public DataResult getGoodsInfo(String goodsName){
        try {
            logger.info("获取商品信息方法开始运行{}");
            List<GoodsVo> goodsInfo =goodsService.getGoodsShInfo(goodsName);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),goodsInfo);
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }
    //删除
    @RequestMapping("deleteBatch")
    @ResponseBody
    public DataResult deleteBatch(@RequestParam("ids[]")Long[] ids){
        try {
            logger.info("商品批量删除方法开始执行{}");
            goodsService.deleteBatch(ids);
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
    @RequestMapping("updateByStatus")
    @ResponseBody
    public DataResult updateByStatus(@RequestParam("ids[]")Long[] ids){
        try {
            logger.info("商品提交审核方法开始执行{}");
            goodsService.updateByAudit(ids);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }

    @RequestMapping("updateByAuditStatus")
    @ResponseBody
    public DataResult updateByAuditStatus(@RequestParam("ids[]")Long[] ids){
        try {
            logger.info("商品提交审核方法开始执行{}");
            goodsService.updateByAuditStatus(ids);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }
    //商品详情
    @RequestMapping("getDetailsById")
    @ResponseBody
    public DataResult getDetailsById(Long id){

        try {
            logger.info("商品提交审核方法开始执行{}");
            List<DetailsVo> detailsVos = goodsService.getDetailsById(id);
            return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),detailsVos);
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
    }
}